﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpaceShooter.Common;

namespace SpaceShooter.Extensions
{
    public static class ListExtension
    {
        public static void ForeachReversed<T>(this IList<T> list, Action<T> action)
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                action(list[i]);
            }
        }

        public static void ForeachReversed<T>(this IList<T> list, Func<T, bool> func)
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (func(list[i]))
                {
                    list.RemoveAt(i);
                }
            }
        }

        public static IList<T> ToList<T>(this IList<object> list)
        {
            var convertedList = new List<T>(list.Count);
            list.Foreach(item => convertedList.Add((T)item));
            return convertedList;
        }

        public static void RemoveMarkedObjected<T>(this IList<T> list)
            where T : GameObject
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (list[i].RemoveFlag)
                {
                    list.RemoveAt(i);
                }
            }
        }

        public static void RemoveMarkedObjected<T>(this IList<T> list, Action<T> removeReporter)
            where T : GameObject
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (list[i].RemoveFlag)
                {
                    removeReporter(list[i]);
                    list.RemoveAt(i);
                }
            }
        }
    }
}
